<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:log="livcos.org/ns/pm/log"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="log pm xs">
  
  <xsl:import href="../../web/style/base.xsl" />

	<xsl:variable name="testTodoUri" select="'/livcos.org/WorkLog/data/ToDo'"/>
	
	<xsl:template match="/">
    <xsl:apply-templates select="document('/livcos.org/WorkLog/data/RecentLogs')/*"/>
  </xsl:template>
  
  <xsl:template match="*">
  </xsl:template>
  
  <xsl:template match="log:log">
    <table border="0" cellspacing="0" id="/livcos.org/WorkLog/data/Log">
      <xsl:apply-templates select="*">
        <xsl:sort select="@time" order="descending"/>
      </xsl:apply-templates>
    </table>
  </xsl:template>
  
  <xsl:template name="logEntry">
    <xsl:param name="symbol"/>
    <xsl:param name="info"/>
    <xsl:param name="action"/>
    <xsl:param name="style"/>
    <tr id="{@id}">
      <xsl:if test="$style">
        <xsl:attribute name="style" select="$style"/>
      </xsl:if>
      <td style="text-align: right">
        <xsl:apply-templates select="@time"/>
      </td>
      <td style="text-align: center">
        <xsl:text>&#160;&#160;</xsl:text>
        <xsl:copy-of select="$symbol"/>
        <xsl:text>&#160;&#160;</xsl:text>
      </td>
      <td>
        <xsl:copy-of select="$info"/>
      </td>
      <td style="text-align: right">
        <xsl:text>&#160;</xsl:text>
        <xsl:copy-of select="$action"/>
        <a style="cursor: pointer" onclick="deleteLog('{@id}')">X</a>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="log:in">
    <xsl:variable name="active"
      select="not(../log:out[(@ref = current()/@ref) and (@time > current()/@time)])"/>
    <xsl:call-template name="logEntry">
      <xsl:with-param name="symbol">-></xsl:with-param>
      <xsl:with-param name="info">
        <xsl:apply-templates select="@ref"/>
      </xsl:with-param>
      <xsl:with-param name="action">
        <xsl:if test="$active">
          <a style="cursor: pointer" onclick="punchOut('{@ref}', 0)">></a>
          <xsl:text>&#160;</xsl:text>
        </xsl:if>
      </xsl:with-param>
      <xsl:with-param name="style">
        <xsl:if test="$active">background-color: #FFAAAA</xsl:if>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="log:out">
    <xsl:call-template name="logEntry">
      <xsl:with-param name="symbol">&lt;-</xsl:with-param>
      <xsl:with-param name="info">
        <xsl:apply-templates select="@ref"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="log:connect">
    <xsl:call-template name="logEntry">
      <xsl:with-param name="symbol">>&lt;</xsl:with-param>
      <xsl:with-param name="info">
        <xsl:apply-templates select="@host"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="log:disconnect">
    <xsl:call-template name="logEntry">
      <xsl:with-param name="symbol">&lt;></xsl:with-param>
      <xsl:with-param name="info">
        <xsl:apply-templates select="@host"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="log:break">
    <xsl:call-template name="logEntry">
      <xsl:with-param name="symbol">...</xsl:with-param>
      <xsl:with-param name="info">
        <xsl:value-of select="@duration"/>
        <xsl:text>min break</xsl:text>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="log:mood">
    <xsl:call-template name="logEntry">
      <xsl:with-param name="symbol">
        <xsl:choose>
          <xsl:when test="@type = 'happy'">:-)</xsl:when>
          <xsl:when test="@type = 'tired'">:()</xsl:when>
          <xsl:when test="@type = 'sad'">:-(</xsl:when>
          <xsl:when test="@type = 'confused'">:-S</xsl:when>
          <xsl:otherwise>-</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
      <xsl:with-param name="info">
        <xsl:value-of select="@type"/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="*" mode="action">
  </xsl:template>
  
  <xsl:template match="log:in" mode="action">
    <xsl:if test="not(../log:out[(@ref = current()/@ref) and (@time > current()/@time)])">
      <a style="cursor: pointer" onclick="punchOut('{@ref}', 0)">></a>
      <xsl:text>&#160;</xsl:text>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="@time">
    <xsl:value-of
      select="format-dateTime(xs:dateTime(.), '[FNn] [D01].[M01].[Y01] [H01]:[m01]:[s01]')"/>
  </xsl:template>
  
  <xsl:template match="@ref">
    <xsl:variable name="obj" select="substring-before(., '#')"/>
    <xsl:variable name="id" select="substring-after(., '#')"/>
    <xsl:apply-templates select="document($obj)//*[@id = $id]"/>
  </xsl:template>
  
  <xsl:template match="pm:task">
    <a style="cursor: pointer"
    	onclick="document.getElementById('ref').value = '{$testTodoUri}#{@id}';">
      <xsl:value-of select="pm:title"/>
    </a>
  </xsl:template>
  
</xsl:stylesheet>
